package com.zhangyue.wqercs.recyclerviewdemo;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

class MyItemDecoration extends RecyclerView.ItemDecoration {
    private Paint paint;
    private int width;

    public MyItemDecoration(){
        //初始化画笔，用来绘制分割线
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        //设置分割线宽度
        width = 5;
    }

    //绘制水平分割线
    private void drawHorizontal(Canvas c,View view) {
        //获取到布局参数。这样可以得到margin属性。我们在绘制时，是需要增加margin属性的
        RecyclerView.LayoutParams layoutParams =
                (RecyclerView.LayoutParams)view.getLayoutParams();
        //分割线就是一个矩形区域。所以我们需要定义一个矩形对象
        //水平分割线就是从左边到右边的距离。并且是在底部绘制。高度刚好是我们开始设置的分割线宽度
        Rect rect = new Rect();
        //矩形左边是itemView的左边
        int left = view.getLeft();
        //矩形右边是itemView的右边
        int right = view.getRight();
        //矩形上边是itemView的下边+下边的外边距（记住：我们是在item的下边新增分割线。）
        int top = view.getBottom()+layoutParams.bottomMargin;
        //矩形的下边刚好是itemView下边+分割线宽度
        int bottom = top+width;
        rect.set(left,top,right,bottom);
        //开始绘制矩形
        c.drawRect(rect,paint);
    }

    private void drawVertical(Canvas c,View view) {
        RecyclerView.LayoutParams layoutParams =
                (RecyclerView.LayoutParams)view.getLayoutParams();
        Rect rect = new Rect();
        int left = view.getRight()+layoutParams.rightMargin;
        int right = left+width;
        int top = view.getTop()-layoutParams.topMargin;
        int bottom = view.getBottom()+layoutParams.bottomMargin;
        rect.set(left,top,right,bottom);
        c.drawRect(rect,paint);
    }

    //测量item的偏移量
    //param1 装饰的区域矩形大小。默认都是0，只需要给参数1赋值对应的大小即可得到装饰大小
    //param2 recyclerView的itemView对象
    //param3 recyclerView对象
    //param4 itemView的状态
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
        outRect.set(0,0,width,width);
    }
    //绘制recycleView的item时会回调该函数。用来装饰item框
    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent,
                       @NonNull RecyclerView.State state) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View view = parent.getChildAt(i);
            //获取每列个数
            int spanCount = ((GridLayoutManager)parent.getLayoutManager()).getSpanCount();
            //当前的i从0开始。所以需要给下标进行加1.如果i%每列个数==0则代表是最后一列，
            //我们就不需要绘制垂直分割线。那么取反，不是最后一列，我们就绘制垂直分割线
            if((i+1)%spanCount != 0){
                drawVertical(c,view);
            }
            //所有item/每列个数就等于总行数
            int rowCount = parent.getChildCount() / spanCount;
            //如果当前i/每列个数<总行数，那么代表它不是最后一行，我们就需要绘制下边的水平分割线
            if(i/spanCount < rowCount){
                drawHorizontal(c,view);
            }
        }
    }
}
